package com.example.teaknow;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Dao.CollectionDao;
import com.example.teaknow.Dao.NewsDao;

import java.text.SimpleDateFormat;
import java.util.Date;

public class NewsDetailActivity extends AppCompatActivity {
    private WebView webView;
    private NewsBean newsBean;
    private NewsDao newsDao;
    private CollectionDao collectionDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        webView= (WebView) findViewById(R.id.wb_new);
        Intent intent=getIntent();
        newsBean=(NewsBean) intent.getSerializableExtra("newsBean");
        initWebView();
        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==R.id.fav){
            if(newsBean.getFlag()==0){
                Toast.makeText(this,"收藏成功",Toast.LENGTH_SHORT).show();
                item.setTitle("取消收藏");
                item.setIcon(getResources().getDrawable(R.drawable.ic_menu_collection2));
                newsBean.setFlag(1);
                newsDao=new NewsDao(NewsDetailActivity.this);
                newsDao.update_Flag(newsBean);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                Date date = new Date(System.currentTimeMillis());
                newsBean.setTime(simpleDateFormat.format(date));
                collectionDao=new CollectionDao(NewsDetailActivity.this);
                collectionDao.insertCollection(newsBean);
            }
            else {
                Toast.makeText(this,"取消收藏",Toast.LENGTH_SHORT).show();
                item.setTitle("收藏");
                item.setIcon(getResources().getDrawable(R.drawable.ic_menu_collection1));
                newsBean.setFlag(0);
                newsDao.update_Flag(newsBean);
                collectionDao.deleteCollection(newsBean);
            }
        }
        else if(item.getItemId()==R.id.action_share){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/*");
            intent.putExtra(Intent.EXTRA_TEXT, webView.getUrl());
            startActivity(intent.createChooser(intent, "选择分享应用"));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //右侧菜单
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.favorite_menu, menu);
        MenuItem menuItem=menu.findItem(R.id.fav);
        if(newsBean.getFlag()==0)
        {
            menuItem.setIcon(getResources().getDrawable(R.drawable.ic_menu_collection1));
            menuItem.setTitle("收藏");
        }
        else
        {
            menuItem.setIcon(getResources().getDrawable(R.drawable.ic_menu_collection2));
            menuItem.setTitle("取消收藏");
        }
        return true;
    }

    private void initWebView() {
        webView.loadUrl(newsBean.getUrl());
        //支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;//返回为true默认webview自带浏览器打开，false调用三方浏览器
            }
        });
    }

}